package com.sgb.kjwl.viewmodel.house;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.lease.LeaseOrderSuccessActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LeaseOrderSuccessModel extends BaseViewModel {
    public ObservableField<Map> map = new ObservableField<>(new HashMap());
    private LeaseOrderSuccessActivity activity;

    public LeaseOrderSuccessModel(LeaseOrderSuccessActivity activity) {
        this.activity = activity;
    }

    /**
     * app跳转到H5
     *
     * @param id
     * @param type
     */
    public void getH5Url(String id, String type,String orderType) {
        String name = "";
        if("1".equals(orderType)){//机械
            if ("1".equals(type)) {
                name = "receivedOrder";//求租订单详情
            } else {
                name = "leaseOrder";//出租订单详情
            }
        }else if("2".equals(orderType)){//设备
            if ("1".equals(type)) {
                name = "receivedOrderFacility";//求租订单详情
            } else {
                name = "equipmentRentOutOrderDetails";//出租订单详情
            }
        }

        DialogHelper.showProgressDialog(activity, null, "加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(name).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    WebViewX5HostActivity.start(activity, url);
                    activity.finish();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
